import re
import time
import pymongo
import constant as c
import common.gyblog as log
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService

LOG = log.get_logger()
SERVICE = ChromeService()


def query_by_keyword(keyword, is_download):
    # 连接数据库
    db_client = pymongo.MongoClient(c.CONNECTION_STR)
    db_database = db_client[c.DATA_BASE]
    db_col = db_database['download_sukebei']

    query = {'keyword': keyword}
    query_ans = list(db_col.find(query))
    len_query = len(query_ans)

    if len_query == 0:
        print('没有查询到{}'.format(keyword))
    else:
        for q in query_ans:
            tor = q['tor']
            print(tor)

            # 插入is_download
            if is_download == 'Y':
                query = {'tor': tor}
                new_values = {"$set": {"is_download": is_download}}
                db_col.update_one(query, new_values)
                LOG.debug('已插入tor in {}'.format(tor))

    print('关键词：{}，查询到{}个'.format(keyword, len_query))
    db_client.close()


if __name__ == '__main__':
    is_download = 'Y'
    keyword = 'C103'
    query_by_keyword(keyword, is_download)
